/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.zyb.common.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.zyb.commons.constant.Constant;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import springfox.documentation.service.Contact;
import java.util.ArrayList;
import java.util.List;
import org.springframework.core.env.Profiles;
import static com.google.common.collect.Lists.newArrayList;
import org.springframework.core.env.Environment;
/**
 * Swagger配置
 *
 *
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig implements WebMvcConfigurer {
    @Autowired
    private Environment environment;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
            .apiInfo(apiInfo())
            .select()
            //加了ApiOperation注解的类，生成接口文档
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            //包下的类，生成接口文档
            .apis(RequestHandlerSelectors.basePackage("com.zyb"))
            .paths(PathSelectors.any())
            .build()
            .directModelSubstitute(java.util.Date.class, String.class);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("谷粒商城")
            .description("gulimall-admin文档")
            .termsOfServiceUrl("https://www.baidu.com")
            .version("3.0.0")
            .build();
    }

    private List<ApiKey> security() {
        return newArrayList(
            new ApiKey(Constant.TOKEN_HEADER, Constant.TOKEN_HEADER, "header")
        );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


//    @Bean
//    public Docket docket() {
//        // 设置显示的swagger环境信息
//        Profiles profiles = Profiles.of("dev", "test");
//        // 判断是否处在自己设定的环境当中
//        boolean flag = environment.acceptsProfiles(profiles);
//
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(apiInfo())
//                .groupName("分组名称")  // 配置api文档的分组
//                .enable(flag)  // 配置是否开启swagger
//                .select()
//                .apis(RequestHandlerSelectors.basePackage("com.zyb")) //配置扫描路径
//                .paths(PathSelectors.any()) // 配置过滤哪些
//                .build();
//    }
//    // api基本信息
//    private ApiInfo apiInfo() {
//        return new ApiInfo("zyb's swagger",
//                "测试swagger-ui",
//                "v1.0",
//                "http://mail.qq.com",
//                new Contact("zyb", "http://mail.qq.com", "593163583@qq.com"),  //作者信息
//                "Apache 2.0",
//                "http://www.apache.org/licenses/LICENSE-2.0",
//                new ArrayList());
//    }

}